import {
	markRaw
} from 'vue'
import InputStart from '@/views/app/chain/nodes/InputStart.vue'
import AppUi from '@/views/app/chain/nodes/AppUi.vue'
// 动作节点
import AiChat from '@/views/app/chain/nodes/action/AiChat.vue'
import FuncCall from '@/views/app/chain/nodes/action/FuncCall.vue'
import FuncChat from '@/views/app/chain/nodes/action/FuncChat.vue'
import KlbSearch from '@/views/app/chain/nodes/action/KlbSearch.vue'
import KlbChat from '@/views/app/chain/nodes/action/KlbChat.vue'
import AIIntention from '@/views/app/chain/nodes/action/AIIntention.vue'
import Rewrite from "@/views/app/chain/nodes/action/Rewrite.vue";
//输出节点
import OutputToClient from '@/views/app/chain/nodes/output/OutputToClient.vue';

const definition = [{
		groupName: "内置节点",
		hide: true,
		list: [{
			name: "开始节点",
			type: "start_user_input",
			component: markRaw(InputStart),
			desc: '接收到用户输入，开始执行流程',
			"defaultConfig": {
				outputField: 'prompt',
			},
			outputFieldNames: ["outputField"]
		}, {
            name: "App应用设置",
            type: "app_config",
            component: markRaw(AppUi),
            desc: '设置应用的相关UI配置',
            "defaultConfig": {
                uiConfig: {
                    userAvatar: '',
                    assistantAvatar: '',
                    appLogo: '',
                    showAvatar: false
                }
            },

        }]
	},
	{
		groupName: "动作节点",
		bgColor: "#F0FDF4",
		color: "#bbf7d0",
		activeColor: "#22c55e",
		list: [{
				"name": "AI Chat",
				"type": "ai_chat",
				"component": markRaw(AiChat),
				"icon": "ri:robot-2-fill",
				"desc": "使用AI 大模型生成回答",
				"defaultConfig": {
					llmConfig: {
						outputField: 'ai_result',
                        outputToResponse:true,
						inputField: null,
						modelType: '',
						systemRole: '',
						promptTpl: null,
						stream: true,
						useContext: true,
						persistDb: true,
						maxTokens: 2000,
						temperature: 0.7,
						topP: 0.95,
						topK: 0,
					}
				},
				outputFieldNames: ["llmConfig.outputField"]
			}, {
                "name": "意图识别",
                // "type": "output",
                "type": "ai_intention",
                "component": markRaw(AIIntention),
                "icon": "gravity-ui:display-pulse",
                "desc": "由AI 根据用户提问,识别用户意图",
                "defaultConfig": {
                    llmConfig: {
                        model: '',
                    },
                    intentionConfig: {
                        outputField: 'intention_result',
                        inputField: null,
                        intentions: []
                    }
                },
                outputFieldNames: ["intentionConfig.outputField"]
            },
			{
				"name": "Function调用",
				// "type": "output",
				"type": "function_call",
				"component": markRaw(FuncCall),
				"icon": "gravity-ui:function",
				"desc": "调用本地函数生成数据",
				"defaultConfig": {
					funcCallConfig: {
						resultOutputField: 'func_call_result',
						matchFuncOutputField: 'match_function',
						functions: [],
					},
					llmConfig: {
						// outputField: 'ai_result',
                        inputField: null,
						model: '',
						systemRole: '',
						promptTpl: null,
						persistDb: false,
						maxTokens: 2000,
						temperature: 0.2,
						topP: 0.5,
						topK: 0,
					}
				},
				outputFieldNames: ["funcCallConfig.resultOutputField", "funcCallConfig.matchFuncOutputField"]
			}, {
                "name": "根据Function调用结果回答",
                "type": "function_chat",
                "component": markRaw(FuncChat),
                "icon": "quill:chat",
                "desc": "结合Function调用的结果,让ai回答用户的问题",
                "defaultConfig": {
                    funcChatConfig: {
                        funcCallResultField: null,
                        matchFunctionField: null,
                        outputField: 'func_chat_result',
                    },
                    llmConfig: {
                        inputField: null,
                        model: null,
                        systemRole: null,
                        promptTpl: null,
                        outputToResponse: true,
                        stream: true,
                        useContext: false,
                        persistDb: true,
                        maxTokens: 2000,
                        temperature: 0.2,
                        topP: 0.95,
                        topK: 0,
                    }
                },
                outputFieldNames: ["funcChatConfig.outputField"]
            },{
				"name": "知识库搜索",
				// "type": "output",
				"type": "klb_search",
				"component": markRaw(KlbSearch),
				"icon": "gravity-ui:database-magnifier",
				"desc": "根据文本去知识库进行检索数据",
				"defaultConfig": {
                    klbSearchConfig: {
						klbId: null,
						inputField: null,
                        minScore: 0.6,
						outputField: 'klb_search_result',
						limit: 6,

					},

				},
				outputFieldNames: ["klbSearchConfig.outputField"]
			},
			{
				"name": "根据知识库结果回答",
				// "type": "output",
				"type": "klb_chat",
				"component": markRaw(KlbChat),
				"icon": "quill:chat",
				"desc": "根据知识库搜索出的结果,让ai回答",
				"defaultConfig": {
					klbChatConfig: {
                        klbResultField: null,
						outputField: 'klb_chat_result',
						onlyKlb: false,
						showDocRefers: true,
						noMatchReply: "抱歉您的问题无法回答",
					},
					llmConfig: {
						inputField: null,
						model: null,
						systemRole: null,
						promptTpl: null,
                        outputToResponse: true,
						stream: true,
						useContext: true,
						persistDb: true,
						maxTokens: 2000,
						temperature: 0.7,
						topP: 0.95,
						topK: 0,
					}
				},
				outputFieldNames: ["klbChatConfig.outputField"]
			}, {
				"name": "重写提问",
				// "type": "output",
				"type": "ai_rewrite",
				"component": markRaw(Rewrite),
				"icon": "akar-icons:arrow-repeat",
				"desc": "根据上下文重新完善提问",
				"defaultConfig": {
					llmConfig: {
                        inputField: null,
						model: '',
						systemRole: '',
						promptTpl: null,
						maxTokens: 2000,
						temperature: 0.7,
						topP: 0.95,
						topK: 0,
					},
					rewriteConfig: {
						outputField: 'rewrite_result',
						limit: 6,

					}
				},
				outputFieldNames: ["rewriteConfig.outputField"]
			},
		]
	},
    {
        groupName: "输出节点",
        bgColor: "rgba(46,141,237,0.18)",
        color: "rgba(46,141,237,0.64)",
        activeColor: "#2e8ded",
        list: [{
            "name": "输出到客户端",
            "type": "output_to_client",
            "component": markRaw(OutputToClient),
            "icon": "jam:phone",
            "desc": "输出内容到客户端",
            "defaultConfig": {
                outputToClientConfig: {
                    persistDb: true,
                    inputField: null,
                    inputTpl: null
                }

            },

        }
        ]
    },

]

/**
 * 根据节点类型获取节点
 * @param type
 * @returns {T|null}
 */
export const getNodeByType = (type) => {
	if (!type) return null;

	for (const group of definition) {
		const node = group.list.find(item => item.type === type)
		if (node) {
			return node;
		}
	}
	return null;
}

/**
 * 节点定义
 * @type {[{groupName: string, hide: boolean, list: [{component: (function(): Promise<*>), name: string, type: string, desc: string}]},{groupName: string, bgColor: string, color: string, activeColor: string, list: [{component: (function(): Promise<*>), name: string, icon: string, type: string, desc: string},{component: (function(): Promise<*>), name: string, icon: string, type: string, desc: string}]}]}
 */
export const NodeDefs = definition;

/**
 * 节点类型
 * @type {*[]}
 */
export const NodeTypes = definition.reduce((prev, curr) => {
	curr.list.forEach(item => {
		prev[item.type] = item.component
	})
	return prev
}, {})

/**
 * 根据节点类型获取输出字段名称
 * @param {string} type 节点类型
 * @returns {string[]} 输出字段名称数组
 */
export const getNodeOutputFieldNames = (type) => {
    const node = getNodeByType(type);
	return node?.outputFieldNames || []
}